package com.netmi.myvideopicker;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.netmi.myvideopicker.bean.VideoItem;
import com.netmi.myvideopicker.databinding.ActivityPlayVideoItemBinding;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：
 * 创建人：tgw
 * 创建时间：2020/7/28
 * 修改备注：
 */
public class VideoPlayFragment extends Fragment {

    private ActivityPlayVideoItemBinding mBinding;
    private List<VideoItem> videoList = new ArrayList<>();
    private int videoPosition = 0;


    //设置EXOplayer播放参数
    //1.默认的播放View
    private PlayerView playerView;
    //    private PlayerControlView playerControlView;//另一个播放界面
    //2.您可以ExoPlayer使用创建实例ExoPlayerFactory。工厂提供了一系列方法来创建ExoPlayer具有不同
    //级别自定义的实例。对于绝大多数用例，ExoPlayerFactory.newSimpleInstance应该使用其中一种 方法。
    //这些方法返回SimpleExoPlayer
    private SimpleExoPlayer player = null;
    // Produces DataSource instances through which media data is loaded.--生成加载媒体数据的数据源实例。
    private DataSource.Factory dataSourceFactory = null;
    // This is the MediaSource representing the media to be played.---这是表示要播放的媒体的媒体源
    private MediaSource videoSource = null;

    public static VideoPlayFragment newInstance(List<VideoItem> videoList, int videoPosition) {
        VideoPlayFragment f = new VideoPlayFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("videoFolder", (Serializable) videoList);
        bundle.putInt("videoPosition", videoPosition);
        f.setArguments(bundle);
        return f;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mBinding == null)
            mBinding = DataBindingUtil.inflate(inflater, R.layout.activity_play_video_item, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            videoList = (List<VideoItem>) getArguments().getSerializable("videoFolder");
            videoPosition = getArguments().getInt("videoPosition");
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (videoList != null && videoList.size() > 0) {
            mBinding.setItem(videoList.get(videoPosition));
            creatVideoPlayer(getActivity());
            prepareVideoSourse(videoList.get(videoPosition).path);
        } else {
            Toast.makeText(getActivity(), "数据源没有", Toast.LENGTH_SHORT).show();
        }
    }

    private void creatVideoPlayer(Activity context) {
        player = new SimpleExoPlayer.Builder(context).build();
        // Bind the player to the view.
        mBinding.playerView.setPlayer(player);
        // Produces DataSource instances through which media data is loaded.
        dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context, "myvideopicker"));
    }

    private void prepareVideoSourse(String videoAddress) {
        Uri mp4VideoUri = Uri.parse(videoAddress);
        player.setRepeatMode(Player.REPEAT_MODE_ONE);//无限循环
        // This is the MediaSource representing the media to be played.
        videoSource =
                new ProgressiveMediaSource.Factory(dataSourceFactory)
                        .createMediaSource(mp4VideoUri);
        // Prepare the player with the source.
        player.prepare(videoSource);
        player.setPlayWhenReady(false);//控制播放暂停开始

    }

    @Override
    public void onPause() {
        super.onPause();
        if (player != null) {
            player.setPlayWhenReady(false);
            player.release();  // 防止内存溢出
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player = null;
        }
    }
}
